package com.ning.parking.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.ning.parking.config.GlobalVariableUtils;
import com.ning.parking.domain.ParkingLotBaseInfo;
import com.ning.parking.domain.ParkingLotBlacklist;
import com.ning.parking.domain.ParkingLotWhitelist;
import com.ning.parking.domain.VO.Pagination;
import com.ning.parking.domain.dto.ParkingLotWhitelistPageDto;
import com.ning.parking.domain.dto.ParkingLotWhitelistSaveUpdateDTO;
import com.ning.parking.service.ParkingLotBaseInfoService;
import com.ning.parking.service.ParkingLotWhitelistService;
import com.ning.parking.service.ParkingRecordService;
import com.scsonic.framework.common.response.R;
import com.scsonic.framework.exception.BizException;
import com.scsonic.framework.util.DateUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ChenN
 * @Date 2025/4/8 15:41
 * @Version 0.0.1
 **/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("whitelist")
public class ParkingLotWhitelistController {
    final ParkingLotWhitelistService parkingLotWhitelistService;
    final ParkingRecordService parkingRecordService;
    final ParkingLotBaseInfoService parkingLotBaseInfoService;

    @PostMapping("saveOrUpdateWhitelist")
    public R<Long> saveOrUpdateWhitelist(@RequestBody ParkingLotWhitelistSaveUpdateDTO dto) {
        ParkingLotWhitelist parkingLotWhitelist = parkingLotWhitelistService.saveOrUpdateWhitelist(dto);
        return R.ok(parkingLotWhitelist.getWhitelistId());
    }

    @PostMapping("saveWhitelist")
    public R<Long> saveWhitelist(@RequestBody ParkingLotWhitelist parkingLotWhitelist) {

        String carLicense = parkingLotWhitelist.getCarLicense();
        if (carLicense == null){
            throw new BizException("carLicense is null");
        }

        List<ParkingLotWhitelist> list = parkingLotWhitelistService.lambdaQuery().eq(ParkingLotWhitelist::getCarLicense, carLicense).eq(ParkingLotWhitelist::getParkingLotId,parkingLotWhitelist.getParkingLotId()).list();

        if (ObjectUtil.isNotEmpty(list)){
            throw new BizException(carLicense+"白名单免费车已存在记录,请搜索后进行编辑");
        }

        DateTime date = DateUtils.date();
        parkingLotWhitelist.setUpdateTime(date);
        parkingLotWhitelist.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotWhitelist.setCreateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotWhitelist.setCreateTime(date);
        ParkingLotWhitelist parkingLotBaseInfoResult = parkingLotWhitelistService.saveWhitelist(parkingLotWhitelist);
        parkingRecordService.updateCarTypeInfo(parkingLotWhitelist);
        return R.ok(parkingLotBaseInfoResult.getWhitelistId());
    }

    @PostMapping("updWhitelist")
    public R<Long> updWhitelist(@RequestBody ParkingLotWhitelist parkingLotWhitelist) {
        if (parkingLotWhitelist.getWhitelistId() == null){
            throw new BizException("WhitelistId is null ");
        }
        DateTime date = DateUtils.date();
        parkingLotWhitelist.setUpdateTime(date);
        parkingLotWhitelist.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotWhitelistService.updateById(parkingLotWhitelist);
        return R.ok(parkingLotWhitelist.getWhitelistId());
    }

    @GetMapping("get-by-whitelist-id")
    public R<ParkingLotWhitelist> getByWhitelistId(ParkingLotWhitelist parkingLotBaseInfo) {
        return R.ok(parkingLotWhitelistService.getById(parkingLotBaseInfo.getWhitelistId()));
    }

    @GetMapping("del")
    public R del( ParkingLotWhitelist parkingLotBlacklist) {
        boolean b = parkingLotWhitelistService.removeById(parkingLotBlacklist.getWhitelistId());
        return R.ok(parkingLotBlacklist.getWhitelistId());
    }

    @GetMapping("page")
    public R<Pagination<ParkingLotWhitelist>> page(ParkingLotWhitelistPageDto parkingLotBaseInfo) {
        String carLicense = parkingLotBaseInfo.getCarLicense();
        if (ObjectUtil.isEmpty(carLicense)){
            parkingLotBaseInfo.setCarLicense(null);
        }
        Pagination<ParkingLotWhitelist> page = parkingLotWhitelistService.selectPage(parkingLotBaseInfo);
        List<ParkingLotWhitelist> records = page.getRecords();
        for (ParkingLotWhitelist record : records) {
            if (record.getParkingLotId()!=null){
                ParkingLotBaseInfo byId = parkingLotBaseInfoService.getById(record.getParkingLotId());
                record.setParkingLotName(byId!=null?byId.getParkingLotName():"--");
                continue;
            }
            record.setParkingLotName("--");
        }
        return R.ok(page);
    }
}
